<?php
namespace App\Repositories\BLL;

use App\Repositories\BLL\Repository;
use App\User;
use Illuminate\Support\Facades\DB;

class UserBLL extends Repository
{
    public function model()
    {
        return User::class;
    }

    public function ajaxIndex($currentRouteName)
    {
        // datatables请求次数
        $draw = request()->input('draw', 1);
        // 开始条数
        $start = request()->input('start', config('admin.global.list.start'));
        // 每页显示数目
        $length = request()->input('length', config('admin.global.list.length'));
        // 排序
        $order['name'] = request()->input('columns.' .request()->input('order.0.column', 0) . '.name');
        $order['dir'] = request()->input('order.0.dir', 'asc');

        $search['value'] = request()->input('search.value', '');

        $search['regex'] = request()->input('search.regex', false);

        $user = $this->model;

        // 搜索框中的值
        if ($search['value']) {
            if ($search['regex'] == 'true') {
                $user = $user->where('name', 'like', "%{$search['value']}%")->orWhere('username', 'like', "%{$search['value']}%");
            } else {
                $user = $user->where('name', $search['value'])->orWhere('username', $search['value']);
            }
        }

        $count = $user->count();

        $user = $user->orderBy($order['name'], $order['dir']);
        $users = $user->offset($start)->limit($length)->get();

        if ($users) {
            foreach ($users as &$v) {
                $v->actionButton = $v->getListActionButton($currentRouteName);
            }
        }

        //datatables固定返回格式
        return [
            'draw' => $draw,
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'data' => $users,
        ];
    }

    public function createEntity($attributes)
    {
        if (array_key_exists('is_super', $attributes) && $attributes['is_super'] == "on") {
            $attributes['is_super']=1;
        }
        $attributes['password'] = bcrypt('123456');
        $userModel = new $this->model;
        $result = $userModel->fill($attributes)->save();
        if (!$result) {
            flash('用户添加失败', 'error');
        }
        //添加角色
        if (array_key_exists('role_id', $attributes)) {
            $roles = $attributes['role_id'];
            foreach ($roles as $role) {
                $userModel->attachRole($role);
            }
        }
        if ($result) {
            flash('添加成功');
        }
        return $result;
    }

    public function editView($id)
    {
        $user = $this->find($id);
        if ($user) {
            return $user;
        }
        abort(404);
    }

    public function updateEntity($attributes, $id)
    {
        if (array_key_exists('is_super', $attributes) && $attributes['is_super'] == "on") {
            $attributes['is_super']=1;
        } else {
            $attributes['is_super']=0;
        }
        $userModel = $this->model->findOrFail($id);
        $userModel->fill($attributes);
        $result = $userModel->save();
        if (!$result) {
            flash('用户修改失败', 'error');
        }
        //删除原始角色
        DB::table('role_user')->where('user_id', $id)->delete();
        //添加角色
        if (array_key_exists('role_id', $attributes)) {
            $roles = $attributes['role_id'];
            foreach ($roles as $role) {
                $userModel->attachRole($role);
            }
        }
        if ($result) {
            flash('添加成功');
        }
        return $result;
    }

    public function destroyEntity($id)
    {
        $result = $this->delete($id);
        if ($result) {
            flash('修改成功');
        } else {
            flash('修改失败', 'error');
        }
        return $result;
    }

    public function resetPsd($id)
    {
        $attr['password'] = bcrypt('123456');
        $result = $this->update($attr, $id);
        if ($result) {
            flash('密码重置成功');
        } else {
            flash('密码重置失败', 'error');
        }
        return $result;
    }

    public function savePersonal($attributes)
    {
        $result = auth()->user()->fill($attributes)->save();
        if ($result) {
            flash('信息保存成功');
        } else {
            flash('信息保存失败', 'error');
        }
        return $result;
    }
    public function resetPassword($attributes)
    {
        $currentUser = auth()->user();

        if ($attributes['newpsd'] !== $attributes['confirmpsd']) {
            flash('两次密码不一致', 'error');
            return false;
        }
        $currentUser->password = bcrypt($attributes['newpsd']);
        $result = $currentUser->save();
        if ($result) {
            flash('密码修改成功');
        } else {
            flash('密码修改失败', 'error');
        }
        return $result;
    }
}
